package com.tg.app.activity.device.add;

import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.tange.module.core.wifi.scan.WiFiScanManager;
import com.tg.app.R;
import com.base.BaseActivity;
import com.tg.app.helper.ActivityHelper;
import com.tg.app.helper.AppHelper;
import com.tg.appcommon.android.DialogUtil;
import com.tg.app.util.LogUtils;
import com.tange.base.toolkit.StatusNavUtils;

import java.util.HashMap;

public  class Car4gGuideActivity extends BaseActivity implements View.OnClickListener {
    public static  final  String TAG  =  "ApGuideFragment";
    private CheckBox box ;
    private Button button;
    private TextView title ,mark,description,help;
    private int type ;
    public static final  String AP_GUIDE_TYPE = "ap_guide_type";
    public static final int DEVICE_RESET = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//remove title bar  即隐藏标题栏
        setContentView(R.layout.fragment_4g_car_guide);
        hideActionBar();
        initView();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            StatusNavUtils.setStatusBarColor(this,getColor(R.color.device_add_background));
        }else {
            StatusNavUtils.setStatusBarColor(this,getResources().getColor(R.color.device_add_background));
        }
        type =getIntent().getIntExtra(AP_GUIDE_TYPE,0);
        HashMap<String,String> map = new HashMap<>();
        map.put("mode","bind_app");
        map.put("event","bind_start");
        LogUtils.uploadLog(map);
       
    }

    @Override
    public void onResume() {
        super.onResume();
        if (WiFiScanManager.scanner().isLocationEnabled(this)){
            WifiManager mWifiManager = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
            if (!mWifiManager.isWifiEnabled()){
                DialogUtil.openWifi(ActivityHelper.getActivityContext(this));
            }else {
                mWifiManager.startScan();
            }
        }else {
            DialogUtil.openGpsService(this);
        }

        if (type ==  DEVICE_RESET){
            resetDevice();
        }
    }

    private void resetDevice(){
        help.setVisibility(View.GONE);
        description.setVisibility(View.GONE);
        title.setText(R.string.device_reset);
    }
    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.btn_before_add_next) {
            if (box.isChecked()) {
                Intent intent = new Intent(getBaseContext(), ApDeviceListActivity.class);
                startActivity(intent);
            }
        } else if (id == R.id.tv_device_ap_add_help) {
            resetDevice();
        }
    }

    @Override
    protected void initView() {
        button =findViewById(R.id.btn_before_add_next);
        title =findViewById(R.id.tv_device_ap_add_title);
        mark =findViewById(R.id.tv_device_wifi_add_title);
        description =findViewById(R.id.tv_preparation_before_add_description);
        help =findViewById(R.id.tv_device_ap_add_help);
        help.setOnClickListener(this);
        button.setOnClickListener(this);
        button.setAlpha(0.4f);
        box = findViewById(R.id.cb_listener);
        box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    button.setAlpha(1f);
                }else {
                    button.setAlpha(0.4f);
                }
            }
        });
        findViewById(R.id.back_toolbar).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        if (AppHelper.isDuomigeApp()){
            AppHelper.setDuoigeText(mark, box, help);
        }
    }
}
